package models

import (
	"fmt"
	"gorm.io/gorm"
)

type Shops struct {
	gorm.Model
	Cate   string  `json:"cate" gorm:"type:varchar(30)"`
	Wheres string  `json:"wheres" gorm:"type:varchar(30)"`
	Start  string  `json:"start" gorm:"type:varchar(50)"`
	Into   string  `json:"into" gorm:"type:varchar(50)"`
	Num    int     `json:"num" gorm:"type:int(10)"`
	Price  float64 `json:"price" gorm:"type:float(10,2)"`
	Who    string  `json:"who" gorm:"type:varchar(30)"`
}

func List(wheres string, page int, pageSize int) (int64, []Shops, error) {
	query := DB.Model(&Shops{})
	if wheres != "" {
		query = query.Where("wheres like ?", "%"+wheres+"%")
	}
	var sum int64
	err = query.Count(&sum).Error
	if err != nil {
		fmt.Println("总条数获取失败")
		return 0, nil, err
	}
	offset := (page - 1) * pageSize
	var shops []Shops
	err = query.Order("price ASC").Offset(offset).Limit(pageSize).Find(&shops).Error
	if err != nil {
		return 0, nil, err
	} else {
		return sum, shops, nil
	}
}
